package com.bxw.forum.service.impl;

import com.bxw.forum.common.AppResult;
import com.bxw.forum.common.ResultCode;
import com.bxw.forum.dao.MessageMapper;
import com.bxw.forum.exception.ApplicationException;
import com.bxw.forum.model.Message;
import com.bxw.forum.model.User;
import com.bxw.forum.service.IMessageService;
import com.bxw.forum.service.IUserService;
import com.bxw.forum.utils.StringUtils;
import com.sun.xml.internal.bind.v2.runtime.SwaRefAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class MessageServiceImpl implements IMessageService {

    @Resource
    private MessageMapper messageMapper;

    @Resource
    private IUserService userService;

    @Override
    public void create(Message message) {
        //非空校验
        //校验接收方是否正常
        //设置默认值
        //调用DAO插入
        //检查是否插入成功

        if(message == null
                || message.getPostUserId() == null
                || message.getReceiveUserId() == null
                || StringUtils.isEmpty(message.getContent())) {

            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());

            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        User user = userService.selectById(message.getReceiveUserId());
        if (user == null || user.getDeleteState() != 0) {
            // 抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        message.setState((byte)0);
        message.setDeleteState((byte)0);
        Date date = new Date();
        message.setCreateTime(date);
        message.setUpdateTime(date);

        int row = messageMapper.insertSelective(message);

        if(row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());

            throw new ApplicationException(AppResult.fail(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public int selectUnreadCount(Long receiveUserId) {

        if(receiveUserId == null || receiveUserId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());

            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        Integer count = messageMapper.selectUnreadCount(receiveUserId);

        if(count == null) {
            log.warn(ResultCode.ERROR_SERVICES.toString());

            throw new ApplicationException(AppResult.fail(ResultCode.ERROR_SERVICES));
        }

        return count;
    }

    @Override
    public List<Message> selectByReceiveUserId(Long receiveUserId) {
        if(receiveUserId == null || receiveUserId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        List<Message> messages = messageMapper.selectByReceiveUserId(receiveUserId);
        return messages;
    }

    @Override
    public void updateStateById(Long id, Byte state) {
        // 非空校验, state : 0 未读 1 已读 2 已回复
        if(state == null || state < 0 || state > 2) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());

            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        Message updateMessage = new Message();
        updateMessage.setId(id);
        updateMessage.setState(state);
        Date date = new Date();
        updateMessage.setUpdateTime(date);

        int row = messageMapper.updateByPrimaryKeySelective(updateMessage);

        if(row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());

            throw new ApplicationException(AppResult.fail(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public Message selectById(Long id) {

        if(id == null || id <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());

            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        Message message = messageMapper.selectByPrimaryKey(id);

        return message;
    }

    @Override
    public void reply(Long repliedId, Message message) {
        if(repliedId == null || repliedId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());

            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        Message existMessage = messageMapper.selectByPrimaryKey(repliedId);

        if(existMessage == null || existMessage.getDeleteState() != 0) {
            log.warn(ResultCode.FAILED_MESSAGE_NOT_EXISTS.toString());

            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_MESSAGE_NOT_EXISTS));
        }

        updateStateById(repliedId,(byte)2);

        create(message);
    }
}
